package algorithm;

public class SelectionSort {

    public static void selectionSort(int[] list) {
        //需要遍历获得最小值的次数
        if (1 >= list.length) return;
        for (int i = 0; i < list.length - 1; i++) {
            int temp = 0;
            int index = i;     //选择当前值为最小值索引
            for (int j = i + 1; j < list.length; j++) {
                if (list[index] > list[j]) {
                    index = j;  //修改最小值索引
                }
            }

            temp = list[index];
            list[index] = list[i];
            list[i] = temp;
        }
    }

    public static void main(String[] args) {
//        int[] list={4,3,6,5,7,8,2,10,2,9};
        int[] list = {3, 5, 9, 1, 4, 2, 7, 6};
        selectionSort(list);
        for (int num : list) {
            System.out.print(num + " ");
        }
    }
}